package com.pur.zsms.unusual.web;

import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.ordercustommanage.vo.SelectMaterVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.unusual.service.IUnusualService;
import com.pur.zsms.unusual.vo.DealUnususlVo;
import com.pur.zsms.unusual.vo.OrderSupplierMaterUnusualVo;

/**
 * 异常模块
 * 
 * @author zhou
 * 
 */
@Controller
@RequestMapping(value = "/unusual")
public class UnusualController {
	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(UnusualController.class);
	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "unusualService")
	private IUnusualService unusualService;

	@RequestMapping(value = "/findUnusualOrder.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findUnusualOrder(@RequestParam(required = false) String page,
	        @RequestParam(required = false) String rows,
	        OrderSupplierMaterUnusualVo orderSupplierMaterUnusualVo) throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = unusualService.findUnusualOrder(pageController, orderSupplierMaterUnusualVo);
		List<OrderSupplierMaterUnusualVo> orderSupplierMaterUnusualVoList = new ArrayList<OrderSupplierMaterUnusualVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderSupplierMaterUnusualVo f = new OrderSupplierMaterUnusualVo();
				f.setOid((Integer) item[0]);
				f.setOrdId((Integer) item[1]);
				f.setOrdCustId((Integer) item[2]);
				f.setOrdCustName((String) item[3]);
				f.setOrdSupId((Integer) item[4]);
				f.setOrdSupName((String) item[5]);
				f.setOrdDate((String) item[6]);
				f.setOrdPaymentState((Integer) item[7]);
				f.setOrdState((Integer) item[8]);
				f.setContent("查看详细异常信息");
				orderSupplierMaterUnusualVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderSupplierMaterUnusualVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * 异常列表 对于客户
	 * */
	@RequestMapping(value = "/findUnusualOrderForCust.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findUnusualOrderForCust(@RequestParam(required = false) String page,
	        @RequestParam(required = false) String rows,
	        OrderSupplierMaterUnusualVo orderSupplierMaterUnusualVo, HttpSession session)
	        throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));

		UserPo userInfo = (UserPo) session.getAttribute("user");
		orderSupplierMaterUnusualVo.setOrdCustId((int) userInfo.getUserId());
		this.pageList = unusualService.findUnusualOrder(pageController, orderSupplierMaterUnusualVo);
		List<OrderSupplierMaterUnusualVo> orderSupplierMaterUnusualVoList = new ArrayList<OrderSupplierMaterUnusualVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderSupplierMaterUnusualVo f = new OrderSupplierMaterUnusualVo();
				f.setOid((Integer) item[0]);
				f.setOrdId((Integer) item[1]);
				f.setOrdCustId((Integer) item[2]);
				f.setOrdCustName((String) item[3]);
				f.setOrdSupId((Integer) item[4]);
				f.setOrdSupName((String) item[5]);
				f.setOrdDate((String) item[6]);
				f.setOrdPaymentState((Integer) item[7]);
				f.setOrdState((Integer) item[8]);
				f.setContent("查看详细异常信息");
				orderSupplierMaterUnusualVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderSupplierMaterUnusualVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	/**
	 * 进入客户详细订单
	 * */
	@RequestMapping(value = "/goUnusualDetailed.do", method = { RequestMethod.GET })
	public ModelAndView goUnusualDetailed(String id, String supId) {
		Map map = new HashMap();
		map.put("ordId", id);
		map.put("supId", supId);
		return new ModelAndView("/unusual/unusualDetailed", map);
	}

	/**
	 * 进入客户详细订单for cust
	 * */
	@RequestMapping(value = "/goUnusualDetailedForCust.do", method = { RequestMethod.GET })
	public ModelAndView goUnusualDetailedForCust(String id, String supId, HttpSession session) {
		String mobile = session.getAttribute("mobile").toString();

		Map map = new HashMap();
		map.put("ordId", id);
		map.put("supId", supId);
		if (mobile.equals("open")) {
			return new ModelAndView("/jsp-mobile/unusual/unusualDetailedForCust_mobile", map);
		} else {
			return new ModelAndView("/unusual/unusualDetailedForCust", map);
		}

	}

	/**
	 * @return
	 */
	@RequestMapping(value = "/findUnusualDetailed.do")
	@ResponseBody
	public String findUnusualDetailed(@RequestParam(required = false) String page,
	        @RequestParam(required = false) String rows,
	        OrderSupplierMaterUnusualVo orderSupplierMaterUnusualVo, HttpSession session, String oid,
	        String supId) throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		UserPo userInfo = (UserPo) session.getAttribute("user");
		orderSupplierMaterUnusualVo.setOrdCustId((int) userInfo.getUserId());
		this.pageList = unusualService.findUnusualDetailed(pageController, orderSupplierMaterUnusualVo, oid,
		        supId);
		List<OrderSupplierMaterUnusualVo> orderSupplierMaterVoList = new ArrayList<OrderSupplierMaterUnusualVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				OrderSupplierMaterUnusualVo f = new OrderSupplierMaterUnusualVo();
				f.setOid(Integer.valueOf(item[0].toString()));
				f.setOrdId(Integer.valueOf(item[1].toString()));
				f.setOrdCustId(Integer.valueOf(item[2].toString()));
				f.setOrdCustName(item[3].toString());
				f.setOrdSupId(Integer.valueOf(item[4].toString()));
				f.setOrdSupName(item[5].toString());
				f.setOrdDate(item[6].toString());
				f.setOrdPaymentState(Integer.valueOf(item[7].toString()));
				f.setOrdState(Integer.valueOf(item[8].toString()));
				f.setOrdMaterId(Integer.valueOf(item[9].toString()));
				f.setOrdMaterName(item[10].toString());
				f.setUnit(item[11].toString());
				f.setBrands(item[12].toString());
				f.setSpecifications(item[13].toString());
				f.setOrdMaterWeightReal(new BigDecimal(item[14].toString()));
				f.setUnusualDeal(Integer.valueOf(item[15].toString()));
				f.setUnusualDealFlag(Integer.valueOf(item[16].toString()));
				f.setUnusualRemark((String) item[17]);
				f.setOrdMaterWeight(new BigDecimal(item[18].toString()));
				f.setUnusualDealDetailed((String) item[19]);
				f.setSelfOrdId(Integer.valueOf(item[20].toString()));

				orderSupplierMaterVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", orderSupplierMaterVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/pulldownUnusualDeal.do", method = { RequestMethod.POST })
	@ResponseBody
	public String pulldownUnusualDeal(int space) {
		JSONObject json = new JSONObject();
		String jsonString = "";
		try {
			jsonString = unusualService.getAllUnusualDealJsonString(space);
		} catch (GenericException e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return jsonString;
	}

	@RequestMapping(value = "/preUnusualDeal.do", method = { RequestMethod.POST })
	public ModelAndView preUnusualDeal(OrderSupplierMaterUnusualVo tOrderSupplierMaterUnusualVo) {
		Map map = new HashMap();
		map.put("rolelist", 1);
		return new ModelAndView("/selectfood/selectMaterForUpdate", map);
	}

	@RequestMapping(value = "/UnusualDealForKeep.do", method = { RequestMethod.POST })
	@ResponseBody
	public String UnusualDealForKeep(SelectMaterVo selectMaterVo) {
		JSONObject json = new JSONObject();
		try {

			// 如果退货 则 设定退货标记
			if (selectMaterVo.getUnusualDeal() == 4) {
				unusualService.orderUnusualDealForReturnGoods(selectMaterVo);

			}
			// 关联订单为0
			selectMaterVo.setSupId(selectMaterVo.getOrdSupId());
			unusualService.orderUnusualDeal(selectMaterVo, 0);
		} catch (GenericException e) {
			e.printStackTrace();
			json.put("success", false);
		}
		json.put("success", true);
		return json.toString();
	}
}
